package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * @author: Liu
 * @date: 2022/02/23
 * @time: 11:20
 */
@Slf4j
@Service
public class EmploeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Override
    public R<Employee> login(Employee employee) {
        /**
         * 参数校验
         * */
        if (ObjectUtils.isEmpty(employee)
                || ObjectUtils.isEmpty(employee.getUsername())
                || ObjectUtils.isEmpty(employee.getPassword())) {
            return R.error("登陆失败！");
        }
        /**
         * 密码转MD5
         */
        String frontpassword = employee.getPassword();
        String md5password = DigestUtils.md5DigestAsHex(frontpassword.getBytes());
        /**
         * 利用mybatisplus从数据库查询获取对象
         */
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee dbemployee = this.getOne(wrapper);
        /**
         * 判断对象是否存在
         *对象不存在，登录失败
         */
        if (ObjectUtils.isEmpty(dbemployee)) {
            return R.error("登陆失败！");
        }
        /**
         * 密码为空或者不一致登陆失败
         */
        if (!dbemployee.getPassword().equals(md5password)) {
            return R.error("登陆失败！");
        }
        /**
         * 激活状态校验
         */
        if (dbemployee.getStatus() != 1) {
            return R.error("登陆失败！");
        }
        /**
         * 置空数据，数据脱敏
         */
        dbemployee.setPassword("null");
        return R.success(dbemployee);
    }

    @Override
    @Transactional
    public R<String> add(Employee employee) {
        if (ObjectUtils.isEmpty(employee)) {
            return R.error("参数非法！");
        }
        if (ObjectUtils.isEmpty(employee.getUsername())
                || ObjectUtils.isEmpty(employee.getName())
                || ObjectUtils.isEmpty(employee.getIdNumber())
                || ObjectUtils.isEmpty(employee.getPhone())) {
            return R.error("参数非法！");
        }

        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);
        employee.setStatus(1);
        /*employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());*/
        this.save(employee);
        return R.success("添加成功！");
    }

    @Override
    public R<Page<Employee>> findByPage(String name, Long page, Long size) {
        if (ObjectUtils.isEmpty(page) || page <= 0) {
            page = 1L;
        }
        if (ObjectUtils.isEmpty(size) || size <= 0) {
            size = 10L;
        }
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(name)) {
            wrapper.like(Employee::getName, name);
        }

        Page<Employee> ipage = new Page<>(page, size);
        Page<Employee> page1 = this.page(ipage, wrapper);
        return R.success(page1);
    }

    @Override
    public R<String> updateStatus(Employee employee) {
        if (ObjectUtils.isEmpty(employee) ||
                ObjectUtils.isEmpty(employee.getId()) ||
                ObjectUtils.isEmpty(employee.getStatus())) {
            return R.error("参数异常！");
        }
        if (employee.getStatus() != 0 && employee.getStatus() != 1) {
            return R.error("参数异常！");
        }

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getId, employee.getId());
        Employee dbEmployee = this.getOne(wrapper);

        if (ObjectUtils.isEmpty(dbEmployee)) {
            return R.error("参数异常！");
        }
        if (dbEmployee.getStatus() == employee.getStatus()) {
            return R.error("参数异常！");
        }
        //employee.setUpdateTime(LocalDateTime.now());
        System.out.println(employee + "------------------------");
        this.updateById(employee);
        return R.success("状态修改成功！");
    }

    @Override
    public R<Employee> findById(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            return R.error("数据查询失败！");
        }

        Employee dbEmployee = this.getById(id);
        if (ObjectUtils.isEmpty(dbEmployee)) {
            return R.error("数据查询失败！");
        }
        return R.success(dbEmployee);
    }

    @Override
    public R<String> updateInfo(Employee employee) {
        if (ObjectUtils.isEmpty(employee.getId())
                || ObjectUtils.isEmpty(employee.getStatus())) {
            return R.error("参数错误！");
        }

        //employee.setUpdateTime(LocalDateTime.now());
        this.saveOrUpdate(employee);
        return R.success("更新成功！");
    }

}
